package br.com.supermercado.web.auth;

import org.hibernate.Session;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

@Name("authenticator")
public class Authenticator {

	@In
	private Session database;

	@In
	private Credentials credentials;

	@In
	private Identity identity;

	public boolean authenticate() {
		/*
		Criteria criteria = database.createCriteria(Usuario.class);
		
		criteria.add(Restrictions.eq("login", credentials.getUsername()));
		criteria.add(Restrictions.eq("password", credentials.getPassword()));
		
		Usuario usuario = (Usuario) criteria.uniqueResult();

		if (usuario == null) {
			System.out.println("treta!!!!");
			
			return false;
			
		}
		
		if (usuario.getRoles() != null) {
			for (Role role : usuario.getRoles()) {
				identity.addRole(role.getName());
				
			}
			
		}*/

		return true;
	}

	@Observer("org.jboss.seam.security.loginSuccessful")
	public void updateUserStats() {
		System.out.println("*** OBSERVER LOGINSUCCESSFUL!!!");
	}
}
